//
//  MainTabBarController.m
//  xxlaugh
//
//  Created by 周立贺 on 16/6/3.
//  Copyright © 2016年 周立贺. All rights reserved.
//

#import "MainTabBarController.h"
#import "MainNavController.h"
#import "EssenceViewController.h"
#import "NewViewController.h"
#import "FriendTrendViewController.h"
#import "MeViewController.h"
@implementation MainTabBarController

+(void)load{
    //设置全局TabBarItem
    [self setupAllTabBarItem];
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    //添加子控制器
    [self addAllChildViewController];
    //设置标题
    [self setTabBarTitle];
    //设置tabBar的背景图片
    self.tabBar.backgroundImage = [UIImage imageNamed:@"TabBarBackImage"];
}

#pragma mark -- 添加子控制器
- (void)addAllChildViewController{
    
    //精华控制器
    EssenceViewController * essenceVC = [[EssenceViewController alloc] init];
    //添加子控制器
    [self addOneChildViewController:essenceVC andBackColor:[UIColor redColor]];
    
    //最新控制器
    NewViewController * newVC = [[NewViewController alloc] init];
    [self addOneChildViewController:newVC andBackColor:[UIColor yellowColor]];
    
    //关注控制器
    FriendTrendViewController * friendTrendVC = [[FriendTrendViewController alloc] init];
    [self addOneChildViewController:friendTrendVC andBackColor:[UIColor greenColor]];
    
    //我的控制器
    MeViewController * meVC = [[MeViewController alloc] init];
    [self addOneChildViewController:meVC andBackColor:[UIColor blueColor]];
    
}
- (void)addOneChildViewController:(UIViewController *)VC andBackColor:(UIColor *)color{
    color = [UIColor whiteColor];
    //设置背景颜色
    VC.view.backgroundColor = color;
    //包装一个导航控制器
    MainNavController * NavVC = [[MainNavController alloc] initWithRootViewController:VC];
    //添加到TabBarVC
    [self addChildViewController:NavVC];
}

#pragma mark -- 设置TabBar图像文字
- (void)setTabBarTitle{
    
    //精华控制器
    UINavigationController * nav1 = self.childViewControllers[0];
    [self setTabBarItemWithVC:nav1 andTitle:@"首页" andimageStr:@"home_normal" andSelectedImageStr:@"home_highlight"];
    
    //最新控制器
    UINavigationController * nav2 = self.childViewControllers[1];
    [self setTabBarItemWithVC:nav2 andTitle:@"最新" andimageStr:@"fishpond_normal" andSelectedImageStr:@"fishpond_highlight"];
    
    //关注控制器
    UINavigationController * nav4 = self.childViewControllers[2];
    [self setTabBarItemWithVC:nav4 andTitle:@"关注" andimageStr:@"message_normal" andSelectedImageStr:@"message_highlight"];
    
    //我的控制器
    UINavigationController * nav5 = self.childViewControllers[3];
    [self setTabBarItemWithVC:nav5 andTitle:@"我的" andimageStr:@"account_normal" andSelectedImageStr:@"account_highlight"];
}
- (void)setTabBarItemWithVC:(UIViewController *)VC andTitle:(NSString *)title andimageStr:(NSString *)imageStr andSelectedImageStr:(NSString *)selectedImageStr{
    
    VC.tabBarItem.title = title;
    VC.tabBarItem.image = [UIImage imageNamed:imageStr];
    VC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageStr];
    
}

#pragma mark -- 设置全局TabBarItem
+ (void)setupAllTabBarItem{
    
    //获取全局的TabBarItem
    UITabBarItem * item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    
    //设置选中状态下文字颜色
    NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0];
    [item setTitleTextAttributes:attrs forState:UIControlStateSelected];
    
    //设置普通状态下字体大小
    NSMutableDictionary * attrsNor = [NSMutableDictionary dictionary];
    attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:9];
    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
    
}
@end
